package com.atguigu.zhxy.service.impl;

import com.atguigu.zhxy.pojo.LoginForm;
import com.atguigu.zhxy.pojo.Student;
import com.atguigu.zhxy.util.MD5;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.zhxy.pojo.Teacher;
import com.atguigu.zhxy.service.TeacherService;
import com.atguigu.zhxy.mapper.TeacherMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

/**
* @author Hoker
* @description 针对表【tb_teacher】的数据库操作Service实现
* @createDate 2024-06-02 10:49:39
*/
@Service
@Transactional
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher>
    implements TeacherService{

    @Override
    public Teacher login(LoginForm loginForm) {
        LambdaQueryWrapper<Teacher> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Teacher::getName, loginForm.getUsername())
                .eq(Teacher::getPassword, MD5.encrypt(loginForm.getPassword()));
        Teacher teacher = baseMapper.selectOne(lambdaQueryWrapper);
        return teacher;
    }

    @Override
    public IPage<Teacher> getTeachersByOpr(Page<Teacher> pageParam, Teacher teacher) {
        QueryWrapper queryWrapper  = new QueryWrapper();
        if (teacher != null){

            String clazzName = teacher.getClazzName();
            String teacherName = teacher.getName();
            if (!StringUtils.isEmpty(clazzName)){
                queryWrapper.like("clazz_name", clazzName);
            }
            if (!StringUtils.isEmpty(teacherName)){
                queryWrapper.eq("name", teacherName);
            }
            queryWrapper.orderByDesc("id");
            queryWrapper.orderByAsc("name");
        }
        IPage<Teacher> page = baseMapper.selectPage(pageParam, queryWrapper);
        return page;
    }

}




